package io.renren.service.impl;

import com.alibaba.fastjson.JSONObject;
import io.renren.entity.SysUserEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.List;
import java.util.Map;

import io.renren.dao.ServiceSettlementMainDao;
import io.renren.entity.ServiceSettlementMainEntity;
import io.renren.service.ServiceSettlementMainService;



@Service("serviceSettlementMainService")
public class ServiceSettlementMainServiceImpl implements ServiceSettlementMainService {
	@Autowired
	private ServiceSettlementMainDao serviceSettlementMainDao;
	
	@Override
	public ServiceSettlementMainEntity queryObject(Integer id){
		return serviceSettlementMainDao.queryObject(id);
	}
	
	@Override
	public List<ServiceSettlementMainEntity> queryList(Map<String, Object> map){
		return serviceSettlementMainDao.queryList(map);
	}
	
	@Override
	public int queryTotal(Map<String, Object> map){
		return serviceSettlementMainDao.queryTotal(map);
	}
	
	@Override
	public void save(ServiceSettlementMainEntity serviceSettlementMain){
		serviceSettlementMainDao.save(serviceSettlementMain);
	}

	@Override
	public void update(ServiceSettlementMainEntity serviceSettlementMain){
		serviceSettlementMainDao.update(serviceSettlementMain);
	}
	
	@Override
	public void delete(Integer id){
		serviceSettlementMainDao.delete(id);
	}
	
	@Override
	public void deleteBatch(Integer[] ids){
		serviceSettlementMainDao.deleteBatch(ids);
	}

	@Override
	public void updateTrailStatus(JSONObject jsonObject) {
		Calendar ca = Calendar.getInstance();
		String type = jsonObject.getString("type");
		String idss=  jsonObject.getString("ids") ;
		SysUserEntity user = (SysUserEntity)jsonObject.get("user");
		String[] ids = idss.substring(1,idss.length()-1).split(",");
		for (Object id : ids) {
			id = id.toString().substring(1, id.toString().length() - 1);
			ServiceSettlementMainEntity entity = serviceSettlementMainDao.queryObject(id);
			if("firTrial".equals(type)){
				if("0".equals(entity.getBasicStatus())){
					entity.setBasicFirstinstanceDate(ca.getTime());
					entity.setBasicFirstinstancePerson(user.getUsername());
					entity.setBasicStatus("1");
				}
			}else if("secTrial".equals(type)){
				if("1".equals(entity.getBasicStatus())){
					entity.setBasicFirstinstanceDate(ca.getTime());
					entity.setBasicFirstinstancePerson(user.getUsername());
					entity.setBasicStatus("2");
				}
			}
			serviceSettlementMainDao.update(entity);
		}
	}

}
